﻿using EFFC.Frame.Net.Base.Common;
using EFFC.Frame.Net.Base.Data;
using EFFC.Frame.Net.Base.Data.Base;
using EFFC.Frame.Net.Global;
using EFFC.Frame.Net.Module.HttpCall;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace XHRPMIS.Common
{
    public class FJWeixinAPI : SimpleRestCallHelper
    {
        public FJWeixinAPI(string url = "", string system_id = "", string system_secret = "")
        {
            ServerUrl = string.IsNullOrEmpty(url) ? MyConfig.GetConfiguration("FJWeixinAPI", "Server_Url") : url;
            System_ID = string.IsNullOrEmpty(system_id) ? MyConfig.GetConfiguration("FJWeixinAPI", "System_ID") : system_id;
            System_Secret = string.IsNullOrEmpty(system_secret) ? MyConfig.GetConfiguration("FJWeixinAPI", "System_Secret") : system_secret;
        }
        /// <summary>
        /// 服务端连接
        /// </summary>
        public string ServerUrl
        {
            get; private set;
        }
        /// <summary>
        /// 系统ID
        /// </summary>
        public string System_ID
        {
            get; private set;
        }
        /// <summary>
        /// 系统Secret
        /// </summary>
        public string System_Secret
        {
            get; private set;
        }
        public bool Send(string route, object data,string method,ref string result,ref string msg)
        {
            FrameDLRObject content = FrameDLRObject.CreateInstance(data, EFFC.Frame.Net.Base.Constants.FrameDLRFlags.SensitiveCase);
            result = base.Send(ServerUrl+ route, content, new
            {
                Authorization = $"Bearer {System_Secret}",
                System_ID = System_ID
            },method);
            GlobalCommon.Logger.WriteLog(EFFC.Frame.Net.Base.Constants.LoggerLevel.DEBUG, $"FJWeixinAPI：\nRequest url:{method} {ServerUrl + route};Response Result={result}");
            if (result.StartsWith("Failed:"))
            {
                msg = result;
                return false;
            }
            var dobj = FrameDLRObject.CreateInstance(result);
            if (dobj.error != null)
            {
                msg = dobj.error;
                return false;
            }
            else
            {
                if (dobj.result != null)
                {
                    if (dobj.result is string)
                    {
                        return true;
                    }
                    else
                    {
                        if (dobj.result.code == "success")
                        {
                            return true;
                        }
                        else
                        {
                            msg = dobj.result.msg;
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
        }
        public bool Get(string route, ref string result, ref string msg)
        {
            result = base.Get(ServerUrl+ route, new
            {
                Authorization = $"Bearer {System_Secret}",
                System_ID = System_ID
            });
            GlobalCommon.Logger.WriteLog(EFFC.Frame.Net.Base.Constants.LoggerLevel.DEBUG, $"FJWeixinAPI：Get End {ServerUrl + route};Response Result={result}");
            if (result.StartsWith("Failed:"))
            {
                msg = result;
                return false;
            }
            var dobj = FrameDLRObject.CreateInstance(result);
            if (dobj.error != null)
            {
                msg = dobj.error;
                return false;
            }
            else
            {
                if (dobj.result != null)
                {
                    if (dobj.result is string)
                    {
                        return true;
                    }
                    else
                    {
                        if (dobj.result.code == "success")
                        {
                            return true;
                        }
                        else
                        {
                            msg = dobj.result.msg;
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 发送取货通知信息
        /// </summary>
        /// <param name="platuid">用户的平台UID</param>
        /// <param name="first">消息中的first内容</param>
        /// <param name="order_no">订单编号</param>
        /// <param name="order_price">订单金额</param>
        /// <param name="product_desc">商品描述</param>
        /// <param name="pick_time">取货时间</param>
        /// <param name="pick_site">取货地点</param>
        /// <param name="remark">备注</param>
        /// <param name="msg">发送失败消息</param>
        /// <param name="tourl">跳转url</param>
        /// <returns></returns>
        public bool SendTemplateMsg(string platuid,
            string first,
            string order_no,
            decimal order_price,
            string product_desc,
            string pick_time,
            string pick_site,
            string remark, 
            ref string msg, 
            string tourl = "")
        {
            var result = "";
            if(Send($"/wx_api/sendtemplatemsg/{platuid}",new
            {
                templateid = "6l20kCUXgLmUbqDgpYBb3XGamfr9CHsQxVXcun91NWo",
                tourl,
                data = new
                {
                    first = new { value = first},
                    keyword1 = new { value = order_no },
                    keyword2 = new { value = order_price },
                    keyword3 = new { value = product_desc },
                    keyword4 = new { value = pick_time },
                    keyword5 = new { value = pick_site },
                    remark = new { value = remark }
                }
            },"Patch",ref result,ref msg))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
